<template>
  <div class="box">
    <!-- 左边 -->
    <div class="left" />
    <!-- 右边 -->
    <div class="right">
      <div class="rtbox">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px" label-position="left">
          <h3 style="margin:0 0 40px 180px;">登录</h3>
          <el-form-item prop="mobile" label="用户名" style="width: 350px;">
            <el-input v-model="ruleForm.mobile" />
          </el-form-item>
          <el-form-item prop="password" label="密码" style="width: 350px;">
            <el-input v-model="ruleForm.password" type="password" show-password />
          </el-form-item>
          <el-form-item prop="isAgree"> <el-checkbox v-model="ruleForm.isAgree">用户平台使用协议</el-checkbox></el-form-item>
          <el-form-item style="width: 350px;">
            <el-button type="primary" style="width: 100%;" @click="doLogin">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        mobile: '13800000002',
        password: '123456',
        isAgree: true
      },
      rules: {
        mobile: [
          // 非空判断
          { required: true, message: '请输入账号', trigger: 'blur' },
          // 正则判断
          { pattern: /^1[3|5|6|7|8|9]\d{9}$/, message: '用户名格式不对', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        // 复选框校验
        isAgree: [
          { validator: (rules, value, callback) => {
            value ? callback() : callback(new Error('必须先同意协议'))
          }, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 1.登录事件
    doLogin() {
      this.$refs.ruleForm.validate(async vali => {
        // if (vali) {
        //   console.log('校验成功')
        //   request.post('/sys/login', {
        //     mobile: this.ruleForm.mobile,
        //     password: this.ruleForm.password
        //   }).then(res => {
        //     console.log('5.登录成功', res)
        //   }).catch(error => {
        //     console.log(error)
        //   })
        // } else {
        //   console.log('校验失败')
        // }

        // 封装登录请求
        if (vali) {
          console.log('校验成功')
          // 捕获业务请求
          // try {
          //   const res = await LoginRequest({
          //     mobile: this.ruleForm.mobile,
          //     password: this.ruleForm.password
          //   })
          //   console.log('5.登录成功', res)
          // } catch (error) {
          //   console.log(error)
          // }

          // 调用vuex里面的actions把token存储起来
          this.$store.dispatch('user/ActionsLogin', {
            mobile: this.ruleForm.mobile,
            password: this.ruleForm.password
          })
        } else {
          console.log('校验失败')
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.box{
  display: flex;

  height: 100vh;
  width: 100%;
.left{
  flex:3;
  background-image: url('~@/assets/common/login_back.png');
  background-repeat: no-repeat;
  background-size: cover;
  /* background-position: 50%; */
}
.right{
  flex:2;
  // background-color: #e6a1fa;
  display: flex;
  justify-content: center;
  align-items: center;
}
}

</style>
